function loadResults() 
{
	var keywords = $("input[name=keywords]").val();
	var type = $("#search-type").val();
		
	$.post("branchAction!getNames.action", {inParma:JSON.stringify({"keywords":keywords,"type":type})}
	, function(data) 
	{
		data = eval("(" + data + ")");
		
		$("#demo").myPagination({
			currPage : data.curPage,
			pageSize : data.pageSize,
			pageCount : data.totalPages,
			ajax:
			{
				on:true,
				url:"branchAction!getNames.action",
				dataType : 'json', // 返回类型
				callback : 'callBack',
				param : 
				{
					"on" : true,
					inParma : JSON.stringify({"keywords":keywords,"type":type})
				},
				ajaxStart : function() 
				{
					return false;
				},
				onClick : function(page) 
				{
					alert(page);
				}
			}
		});		
		
	});
}

function callBack(data) 
{	
	$("#listTable>tbody").children().remove();
	data = eval("(" + data + ")");
	
	if (data.found == 0)
	{
		alert("未找到相关数据");
	} 
	else
	{
		$(data.data).each
		(
				function(ix, ite) 
				{
					var trStr = '<tr><td><label><input type="radio" name="radios" id="'+ix + '"' + 
					' value="' + ite.id + '" onClick="loadBranch(' + ite.id + ')"">'+ite.name + '</label></td></tr>';
					
					$("#listTable>tbody").append(trStr);
				});
	}
	
}

function loadBranch(rootId)
{	
	$.post("branchAction!getOrgInfo.action", {inParma:JSON.stringify({"rootId":rootId})}
	, function(data)
	{
		data = eval("(" + data + ")");
		
		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{			
			showBranches(data.data);
			
			$("#branchMenu").css({ "border-color": "black" }); 
		}
	});
}

var setting = {    
        check:{
            enable:true
        },        
        data:{
            simpleData:{
                enable:true
            }
        },
        callback:{
        	onRightClick:onRightClick
        }
    };

function showBranches(zNodeData)
{
	$.fn.zTree.init($("#units"), setting, zNodeData);
}

function onRightClick(event,treeId,treeNode)
{
	if (treeNode != null)
	{
		zTree = $.fn.zTree.getZTreeObj("units");
		zTree.selectNode(treeNode);
		//zTree.checkNode(treeNode,true);
		showRMenu(event.clientX,event.clientY);
		$("body").bind("mousedown", onBodyMouseDown);
	}
	
}

function onBodyMouseDown(event)
{  
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0))
    {  
    	$("#rMenu").css({ "visibility": "hidden" });  
    }
    
    if (!(event.target.id == "inputDiv" || $(event.target).parents("#inputDiv").length > 0))
    {  
        $("#inputDiv").css({ "visibility": "hidden" });  
    }
}  

function showRMenu(x,y)
{
	$("#rMenu").show();
	$("#rMenu").css({ "top": y + "px", "left": x + "px", "visibility": "visible" });
	
}

function showInput(x,y)
{
	$("#inputDiv").show();
	$("#inputDiv").css({ "top": y + "px", "left": x + "px", "visibility": "visible" });
	
}

function removeTreeNode()
{
	$("#rMenu").hide();
	delNode();
    
}

function editName()
{
	$("#rMenu").hide();
	
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	var orgName = nodes[0].name;
	//alert("orgName－－＞"+orgName);
	$("#orgName").val(orgName);
	
    //显示新增div  
    var tag = 0; 
    
    $("#flag").val("chg");
    
    $("#units").mousemove(function (e) 
		{  
        	if (tag == 0) 
        		showInput(e.pageX, e.pageY);  
        	
        	$("#orgName").focus();
        	tag = tag + 1;  
    });
}

function addTreeNode() 
{  
    $("#rMenu").hide();
    //显示新增div  
    var tag = 0; 
    
    $("#flag").val("add");
    
    $("#units").mousemove(function (e) 
		{  
        	if (tag == 0) 
        		showInput(e.pageX, e.pageY);  
        	
        	$("#orgName").focus();
        	tag = tag + 1;  
    });
}

function submit()
{
	var flag = $("#flag").val();
	var orgName = $("#orgName").val();
	
	if (orgName == "")
	{
		alert("名称不能为空！");
	}
	else
	{
		if(flag == "add")
		{		
			addNode();		
		}
		else if (flag == "chg")
		{		
			updateNode();
		}
		else
		{
			alert("未识别的操作");
		}
	}
	
}

function getRoot() 
{
   var treeObj = $.fn.zTree.getZTreeObj("units");
    //返回一个根节点
   return treeObj.getNodesByFilter(function (node) { return node.level == 0 ;}, true);
}

function updateNode()
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	var id = nodes[0].id;
	var orgName = $("#orgName").val();
	//alert("id-->" +id + " orgName-->" + orgName);
	
	$("#orgName").val("");
	$("#inputDiv").hide();
	
	$.post("branchAction!updateOrganization.action", {inParma:JSON.stringify({"orgName":orgName,"id":id})}
	, function(data)
	{
		data = eval("(" + data + ")");
		var rootNode = getRoot();
		var rootId = rootNode.id;
						
		if (data.ok == 0)
		{
			alert("更新失败");
		}
		else
		{			
			alert("更新成功");
		}
		
		loadBranch(rootId);
	});
}

function delNode()
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	
	if (nodes[0].isParent)
	{
		alert("请逐个删除下级单位后再删除该单位");
	}
	else
	{
		var id = nodes[0].id;
		
		$.post("branchAction!delOrganization.action", {inParma:JSON.stringify({"id":id})}
		, function(data)
		{
			data = eval("(" + data + ")");
			var rootNode = getRoot();
			var rootId = rootNode.id;
							
			if (data.ok == 0)
			{
				alert("删除失败");
			}
			else
			{			
				alert("删除成功");
			}
			
			loadBranch(rootId);
		});
	}
	
}

function addNode()
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	var pId = nodes[0].id;
	var orgName = $("#orgName").val();
	alert("pId-->" +pId + " orgName-->" + orgName);
	
	$("#orgName").val("");
	$("#inputDiv").hide();
	
	$.post("branchAction!addOrganization.action", {inParma:JSON.stringify({"orgName":orgName,"pId":pId})}
	, function(data)
	{
		data = eval("(" + data + ")");
		var rootNode = getRoot();
		var rootId = rootNode.id;
						
		if (data.ok == 0)
		{
			alert("新增失败");
		}
		else
		{			
			alert("新增成功");
		}
		
		loadBranch(rootId);
	});
}
